layui.use(['layer','form','upload'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,upload = layui.upload;

    form.verify({
        username:function (value) {
            if(value.length<3){
                return "用户名长度必须大于3位";
            }
        },
        password: [/^[\S]{6,12}$/,'密码必须6到12位，且不能出现空格']
    });

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#update_cover_btn'
        ,url: '/admin/project/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.flag;
            var message = res.message;
            if(!flag){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                layer.msg("收款码上传成功!");
                $("#cover_img").attr("value",res.data.url);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    form.on('submit(*)',function (data) {

        var password = $("#passrod").val();
        var repwd = $("#repassword").val();
        if(password != repwd){
            layer.msg("两次密码不一样！");
            return;
        }

        var index =   layer.msg('注册...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });
        $.ajax({
            url:data.form.action,
            type:data.form.method,
            data:$(data.form).serialize(),
            success:function (result) {
                var flag = result.flag;
                if(flag){
                    parent.layer.closeAll();
                    parent.layer.msg("注册成功！");
                    // layer.confirm('注册成功！', function(index){
                    //     parent.layer.closeAll();
                    // });
                }else{
                    layer.msg(result.message);
                }
            },
            error:function () {
                layer.msg("系统异常！");
            }

        })

    })

});

function getVerify(obj) {
    obj.src = "/getVerify?"+Math.random();
}

function showRegisterView() {
    //多窗口模式，层叠置顶
    layer.open({
        type: 2 //此处以iframe举例
        ,title: '注册您的账户！'
        ,area: ['390px', '460px']
        ,shade: 0
        ,maxmin: true
        ,content: '/registerView'
        ,btn: ['保存'] //只是为了演示
        ,yes: function(){

        }
    });
}